﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    internal class ColorPicker : EditorWindow
    {
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$mapB;
        private const int kColorBoxSize = 8;
        private const int kHueRes = 0x40;
        [SerializeField]
        private float m_A = 1f;
        [SerializeField]
        private Texture2D m_AlphaTexture;
        [SerializeField]
        private float m_B;
        private Texture2D m_BTexture;
        private float m_BTextureG = -1f;
        private float m_BTextureR = -1f;
        [SerializeField]
        private Color m_Color = Color.black;
        [SerializeField]
        private Texture2D m_ColorBox;
        [SerializeField]
        private ColorBoxMode m_ColorBoxMode = ColorBoxMode.BG_R;
        [SerializeField]
        private Color[] m_Colors;
        [SerializeField]
        private Texture2D m_ColorSlider;
        [SerializeField]
        private float m_ColorSliderSize = 4f;
        [SerializeField]
        private GUIView m_DelegateView;
        [SerializeField]
        private float m_G;
        private Texture2D m_GTexture;
        private float m_GTextureB = -1f;
        private float m_GTextureR = -1f;
        [SerializeField]
        private float m_H;
        [SerializeField]
        private Texture2D m_HueTexture;
        private float m_HueTextureS = -1f;
        private float m_HueTextureV = -1f;
        [SerializeField]
        private bool m_IsOSColorPicker;
        [SerializeField]
        private float m_LastConstant = -1f;
        [SerializeField]
        private Vector2 m_LastConstantValues = new Vector2(-1f, -1f);
        private float m_OldAlpha = -1f;
        [SerializeField]
        private ColorBoxMode m_OldColorBoxMode;
        [SerializeField]
        private Color m_OriginalColor;
        [SerializeField]
        private float m_R;
        [SerializeField]
        private bool m_resetKeyboardControl;
        private Texture2D m_RTexture;
        private float m_RTextureB = -1f;
        private float m_RTextureG = -1f;
        [SerializeField]
        private float m_S;
        [SerializeField]
        private Texture2D m_SatTexture;
        private float m_SatTextureH = -1f;
        private float m_SatTextureV = -1f;
        [SerializeField]
        private bool m_ShowAlpha = true;
        [SerializeField]
        private bool m_ShowColors = true;
        [SerializeField]
        private bool m_ShowSliders = true;
        [SerializeField]
        private SliderMode m_SliderMode = SliderMode.HSV;
        [SerializeField]
        private float m_SliderValue;
        [NonSerialized]
        private int m_TextureColorBoxMode = -1;
        [SerializeField]
        private int m_TextureColorSliderMode = -1;
        [SerializeField]
        private ContainerWindow m_TrackingWindow;
        [SerializeField]
        private float m_V;
        [SerializeField]
        private Texture2D m_ValTexture;
        private float m_ValTextureH = -1f;
        private float m_ValTextureS = -1f;
        private static ColorPicker s_SharedColorPicker;
        private static int s_Slider2Dhash = "Slider2D".GetHashCode();
        private static Styles styles;

        public ColorPicker()
        {
            base.hideFlags = HideFlags.DontSave;
            this.m_ShowSliders = EditorPrefs.GetInt("CPSliderShow", 1) != 0;
            this.m_SliderMode = (SliderMode) EditorPrefs.GetInt("CPSliderMode", 0);
            this.m_ShowColors = EditorPrefs.GetInt("CPColorShow", 1) != 0;
            this.m_ColorBoxMode = (ColorBoxMode) EditorPrefs.GetInt("CPColorMode", 0);
            this.m_IsOSColorPicker = EditorPrefs.GetBool("UseOSColorPicker");
            EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(this.PollOSColorPicker));
        }

        private void CalcHeight()
        {
            int[] numArray = new int[] { 0x67, 160, 0xff, 0x138 };
            int num = numArray[(!this.m_ShowSliders ? 0 : 1) + (!this.m_ShowColors ? 0 : 2)];
            if (!this.m_ShowAlpha)
            {
                num -= 0x11;
            }
            this.SetHeight((float) num);
        }

        private void DrawColorSlider(Rect colorSliderRect, Vector2 constantValues)
        {
            if (Event.current.type == EventType.Repaint)
            {
                if (this.m_ColorBoxMode != this.m_TextureColorSliderMode)
                {
                    int width = 0;
                    int height = 0;
                    width = (int) this.m_ColorSliderSize;
                    if (this.m_ColorBoxMode == ColorBoxMode.SV_H)
                    {
                        height = 0x40;
                    }
                    else
                    {
                        height = (int) this.m_ColorSliderSize;
                    }
                    if (this.m_ColorSlider == null)
                    {
                        this.m_ColorSlider = MakeTexture(width, height);
                    }
                    if ((this.m_ColorSlider.width != width) || (this.m_ColorSlider.height != height))
                    {
                        this.m_ColorSlider.Resize(width, height);
                    }
                }
                if ((this.m_ColorBoxMode != this.m_TextureColorSliderMode) || (constantValues != this.m_LastConstantValues))
                {
                    Color[] pixels = this.m_ColorSlider.GetPixels(0);
                    int xSize = this.m_ColorSlider.width;
                    int ySize = this.m_ColorSlider.height;
                    switch (this.m_ColorBoxMode)
                    {
                        case ColorBoxMode.SV_H:
                            FillArea(xSize, ySize, pixels, new Color(0f, 1f, 1f, 1f), new Color(0f, 0f, 0f, 0f), new Color(1f, 0f, 0f, 0f));
                            HSVToRGBArray(pixels);
                            break;

                        case ColorBoxMode.HV_S:
                            FillArea(xSize, ySize, pixels, new Color(this.m_H, 0f, Mathf.Max(this.m_V, 0.3f), 1f), new Color(0f, 0f, 0f, 0f), new Color(0f, 1f, 0f, 0f));
                            HSVToRGBArray(pixels);
                            break;

                        case ColorBoxMode.HS_V:
                            FillArea(xSize, ySize, pixels, new Color(this.m_H, this.m_S, 0f, 1f), new Color(0f, 0f, 0f, 0f), new Color(0f, 0f, 1f, 0f));
                            HSVToRGBArray(pixels);
                            break;

                        case ColorBoxMode.BG_R:
                            FillArea(xSize, ySize, pixels, new Color(0f, this.m_G, this.m_B, 1f), new Color(0f, 0f, 0f, 0f), new Color(1f, 0f, 0f, 0f));
                            break;

                        case ColorBoxMode.BR_G:
                            FillArea(xSize, ySize, pixels, new Color(this.m_R, 0f, this.m_B, 1f), new Color(0f, 0f, 0f, 0f), new Color(0f, 1f, 0f, 0f));
                            break;

                        case ColorBoxMode.RG_B:
                            FillArea(xSize, ySize, pixels, new Color(this.m_R, this.m_G, 0f, 1f), new Color(0f, 0f, 0f, 0f), new Color(0f, 0f, 1f, 0f));
                            break;
                    }
                    this.m_ColorSlider.SetPixels(pixels, 0);
                    this.m_ColorSlider.Apply(true);
                }
                Graphics.DrawTexture(colorSliderRect, this.m_ColorSlider, new Rect(0.5f / ((float) this.m_ColorSlider.width), 0.5f / ((float) this.m_ColorSlider.height), 1f - (1f / ((float) this.m_ColorSlider.width)), 1f - (1f / ((float) this.m_ColorSlider.height))), 0, 0, 0, 0, Color.grey);
            }
        }

        private void DrawColorSpaceBox(Rect colorBoxRect, float constantValue)
        {
            if (Event.current.type == EventType.Repaint)
            {
                if (this.m_ColorBoxMode != this.m_TextureColorBoxMode)
                {
                    int width = 0;
                    int height = 0;
                    height = 8;
                    if ((this.m_ColorBoxMode == ColorBoxMode.HV_S) || (this.m_ColorBoxMode == ColorBoxMode.HS_V))
                    {
                        width = 0x40;
                    }
                    else
                    {
                        width = 8;
                    }
                    if (this.m_ColorBox == null)
                    {
                        this.m_ColorBox = MakeTexture(width, height);
                    }
                    if ((this.m_ColorBox.width != width) || (this.m_ColorBox.height != height))
                    {
                        this.m_ColorBox.Resize(width, height);
                    }
                }
                if ((this.m_ColorBoxMode != this.m_TextureColorBoxMode) || (this.m_LastConstant != constantValue))
                {
                    this.m_Colors = this.m_ColorBox.GetPixels(0);
                    int xSize = this.m_ColorBox.width;
                    int ySize = this.m_ColorBox.height;
                    switch (this.m_ColorBoxMode)
                    {
                        case ColorBoxMode.SV_H:
                            FillArea(xSize, ySize, this.m_Colors, new Color(this.m_H, 0f, 0f, 1f), new Color(0f, 1f, 0f, 0f), new Color(0f, 0f, 1f, 0f));
                            HSVToRGBArray(this.m_Colors);
                            break;

                        case ColorBoxMode.HV_S:
                            FillArea(xSize, ySize, this.m_Colors, new Color(0f, this.m_S, 0f, 1f), new Color(1f, 0f, 0f, 0f), new Color(0f, 0f, 1f, 0f));
                            HSVToRGBArray(this.m_Colors);
                            break;

                        case ColorBoxMode.HS_V:
                            FillArea(xSize, ySize, this.m_Colors, new Color(0f, 0f, this.m_V, 1f), new Color(1f, 0f, 0f, 0f), new Color(0f, 1f, 0f, 0f));
                            HSVToRGBArray(this.m_Colors);
                            break;

                        case ColorBoxMode.BG_R:
                            FillArea(xSize, ySize, this.m_Colors, new Color(this.m_R, 0f, 0f, 1f), new Color(0f, 0f, 1f, 0f), new Color(0f, 1f, 0f, 0f));
                            break;

                        case ColorBoxMode.BR_G:
                            FillArea(xSize, ySize, this.m_Colors, new Color(0f, this.m_G, 0f, 1f), new Color(0f, 0f, 1f, 0f), new Color(1f, 0f, 0f, 0f));
                            break;

                        case ColorBoxMode.RG_B:
                            FillArea(xSize, ySize, this.m_Colors, new Color(0f, 0f, this.m_B, 1f), new Color(1f, 0f, 0f, 0f), new Color(0f, 1f, 0f, 0f));
                            break;
                    }
                    this.m_ColorBox.SetPixels(this.m_Colors, 0);
                    this.m_ColorBox.Apply(true);
                    this.m_LastConstant = constantValue;
                    this.m_TextureColorBoxMode = (int) this.m_ColorBoxMode;
                }
                Graphics.DrawTexture(colorBoxRect, this.m_ColorBox, new Rect(0.5f / ((float) this.m_ColorBox.width), 0.5f / ((float) this.m_ColorBox.height), 1f - (1f / ((float) this.m_ColorBox.width)), 1f - (1f / ((float) this.m_ColorBox.height))), 0, 0, 0, 0, Color.grey);
            }
        }

        private static void FillArea(int xSize, int ySize, Color[] retval, Color topLeftColor, Color rightGradient, Color downGradient)
        {
            Color color = new Color(0f, 0f, 0f, 0f);
            Color color2 = new Color(0f, 0f, 0f, 0f);
            if (xSize > 1)
            {
                color = (Color) (rightGradient / ((float) (xSize - 1)));
            }
            if (ySize > 1)
            {
                color2 = (Color) (downGradient / ((float) (ySize - 1)));
            }
            Color color3 = topLeftColor;
            int num = 0;
            for (int i = 0; i < ySize; i++)
            {
                Color color4 = color3;
                for (int j = 0; j < xSize; j++)
                {
                    retval[num++] = color4;
                    color4 += color;
                }
                color3 += color2;
            }
        }

        private void HSVSliders()
        {
            bool changed = GUI.changed;
            GUI.changed = false;
            this.m_HueTexture = Update1DSlider(this.m_HueTexture, 0x40, 1f, 1f, ref this.m_HueTextureS, ref this.m_HueTextureV, 0, true);
            this.m_SatTexture = Update1DSlider(this.m_SatTexture, 8, this.m_H, Mathf.Max(this.m_V, 0.2f), ref this.m_SatTextureH, ref this.m_SatTextureV, 1, true);
            this.m_ValTexture = Update1DSlider(this.m_ValTexture, 8, this.m_H, this.m_S, ref this.m_ValTextureH, ref this.m_ValTextureS, 2, true);
            float val = (int) Mathf.Round(this.m_H * 359f);
            float num2 = (int) Mathf.Round(this.m_S * 255f);
            float num3 = (int) Mathf.Round(this.m_V * 255f);
            val = this.TexturedSlider(this.m_HueTexture, "H", val, 0f, 359f);
            num2 = this.TexturedSlider(this.m_SatTexture, "S", num2, 0f, 255f);
            num3 = this.TexturedSlider(this.m_ValTexture, "V", num3, 0f, 255f);
            if (GUI.changed)
            {
                this.m_H = val / 359f;
                this.m_S = num2 / 255f;
                this.m_V = num3 / 255f;
                this.HSVToRGB();
            }
            GUI.changed |= changed;
        }

        private void HSVToRGB()
        {
            Color color = EditorGUIUtility.HSVToRGB(this.m_H, this.m_S, this.m_V);
            this.m_R = color.r;
            this.m_G = color.g;
            this.m_B = color.b;
        }

        private static void HSVToRGBArray(Color[] colors)
        {
            int length = colors.Length;
            for (int i = 0; i < length; i++)
            {
                Color color = colors[i];
                Color color2 = EditorGUIUtility.HSVToRGB(color.r, color.g, color.b);
                color2.a = color.a;
                colors[i] = color2;
            }
        }

        public static Texture2D MakeTexture(int width, int height)
        {
            return new Texture2D(width, height, TextureFormat.ARGB32, false) { hideFlags = HideFlags.HideAndDontSave, wrapMode = TextureWrapMode.Clamp, hideFlags = HideFlags.DontSave };
        }

        public void OnDestroy()
        {
            if (this.m_ColorSlider != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_ColorSlider);
            }
            if (this.m_ColorBox != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_ColorBox);
            }
            if (this.m_RTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_RTexture);
            }
            if (this.m_GTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_GTexture);
            }
            if (this.m_BTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_BTexture);
            }
            if (this.m_HueTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_HueTexture);
            }
            if (this.m_SatTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_SatTexture);
            }
            if (this.m_ValTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_ValTexture);
            }
            if (this.m_AlphaTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_AlphaTexture);
            }
            s_SharedColorPicker = null;
            if (this.m_IsOSColorPicker)
            {
                OSColorPicker.Close();
            }
            EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.PollOSColorPicker));
        }

        private void OnGUI()
        {
            if (styles == null)
            {
                styles = new Styles();
            }
            if (this.m_resetKeyboardControl)
            {
                GUIUtility.keyboardControl = 0;
                this.m_resetKeyboardControl = false;
            }
            if (Event.current.type == EventType.ExecuteCommand)
            {
                string commandName = Event.current.commandName;
                if (commandName != null)
                {
                    int num;
                    if (<>f__switch$mapB == null)
                    {
                        Dictionary<string, int> dictionary = new Dictionary<string, int>(3);
                        dictionary.Add("EyeDropperUpdate", 0);
                        dictionary.Add("EyeDropperClicked", 1);
                        dictionary.Add("EyeDropperCancelled", 2);
                        <>f__switch$mapB = dictionary;
                    }
                    if (<>f__switch$mapB.TryGetValue(commandName, out num))
                    {
                        switch (num)
                        {
                            case 0:
                                base.Repaint();
                                break;

                            case 1:
                            {
                                Color lastPickedColor = EyeDropper.GetLastPickedColor();
                                this.m_R = lastPickedColor.r;
                                this.m_G = lastPickedColor.g;
                                this.m_B = lastPickedColor.b;
                                this.RGBToHSV();
                                this.m_ColorBoxMode = this.m_OldColorBoxMode;
                                this.m_Color = new Color(this.m_R, this.m_G, this.m_B, this.m_A);
                                this.SendEvent(true);
                                break;
                            }
                            case 2:
                                base.Repaint();
                                this.m_ColorBoxMode = this.m_OldColorBoxMode;
                                break;
                        }
                    }
                }
            }
            EditorGUIUtility.LookLikeControls(15f, 30f);
            GUILayout.BeginVertical(styles.background, new GUILayoutOption[0]);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(40f), GUILayout.ExpandWidth(false) };
            if (GUILayout.Button(styles.eyeDropper, GUIStyle.none, options))
            {
                EyeDropper.Start(base.m_Parent);
                this.m_ColorBoxMode = ColorBoxMode.EyeDropper;
                GUIUtility.ExitGUI();
            }
            Color color2 = new Color(this.m_R, this.m_G, this.m_B, this.m_A);
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.ExpandWidth(true) };
            Rect position = GUILayoutUtility.GetRect(20f, 20f, 20f, 20f, styles.colorPickerBox, optionArray2);
            EditorGUIUtility.DrawColorSwatch(position, color2, this.m_ShowAlpha);
            if (Event.current.type == EventType.Repaint)
            {
                styles.pickerBox.Draw(position, GUIContent.none, false, false, false, false);
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(10f);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.m_ShowColors = GUILayout.Toggle(this.m_ShowColors, styles.colorToggle, EditorStyles.foldout, new GUILayoutOption[0]);
            GUI.enabled = this.m_ShowColors;
            GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            if (GUILayout.Button(styles.colorCycle, GUIStyle.none, optionArray3))
            {
                this.m_OldColorBoxMode = this.m_ColorBoxMode = (this.m_ColorBoxMode + 1) % ColorBoxMode.EyeDropper;
            }
            GUI.enabled = true;
            GUILayout.EndHorizontal();
            if (this.m_ShowColors)
            {
                bool changed = GUI.changed;
                GUILayoutOption[] optionArray4 = new GUILayoutOption[] { GUILayout.ExpandHeight(false) };
                GUILayout.BeginHorizontal(optionArray4);
                GUILayoutOption[] optionArray5 = new GUILayoutOption[] { GUILayout.MinWidth(64f), GUILayout.MinHeight(64f), GUILayout.MaxWidth(256f), GUILayout.MaxHeight(256f) };
                Rect boxPos = GUILayoutUtility.GetAspectRect(1f, styles.pickerBox, optionArray5);
                EditorGUILayout.Space();
                GUILayoutOption[] optionArray6 = new GUILayoutOption[] { GUILayout.ExpandHeight(true) };
                Rect sliderPos = GUILayoutUtility.GetRect(8f, 32f, 64f, 64f, styles.pickerBox, optionArray6);
                GUILayout.EndHorizontal();
                GUI.changed = false;
                switch (this.m_ColorBoxMode)
                {
                    case ColorBoxMode.SV_H:
                        this.Slider3D(boxPos, sliderPos, ref this.m_S, ref this.m_V, ref this.m_H, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.HSVToRGB();
                        }
                        break;

                    case ColorBoxMode.HV_S:
                        this.Slider3D(boxPos, sliderPos, ref this.m_H, ref this.m_V, ref this.m_S, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.HSVToRGB();
                        }
                        break;

                    case ColorBoxMode.HS_V:
                        this.Slider3D(boxPos, sliderPos, ref this.m_H, ref this.m_S, ref this.m_V, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.HSVToRGB();
                        }
                        break;

                    case ColorBoxMode.BG_R:
                        this.Slider3D(boxPos, sliderPos, ref this.m_B, ref this.m_G, ref this.m_R, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.RGBToHSV();
                        }
                        break;

                    case ColorBoxMode.BR_G:
                        this.Slider3D(boxPos, sliderPos, ref this.m_B, ref this.m_R, ref this.m_G, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.RGBToHSV();
                        }
                        break;

                    case ColorBoxMode.RG_B:
                        this.Slider3D(boxPos, sliderPos, ref this.m_R, ref this.m_G, ref this.m_B, styles.pickerBox, styles.thumb2D, styles.thumbVert);
                        if (GUI.changed)
                        {
                            this.RGBToHSV();
                        }
                        break;

                    case ColorBoxMode.EyeDropper:
                        EyeDropper.DrawPreview(Rect.MinMaxRect(boxPos.x, boxPos.y, sliderPos.xMax, boxPos.yMax));
                        break;
                }
                EditorGUILayout.Space();
                GUI.changed |= changed;
            }
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.m_ShowSliders = GUILayout.Toggle(this.m_ShowSliders, styles.sliderToggle, EditorStyles.foldout, new GUILayoutOption[0]);
            GUI.enabled = this.m_ShowSliders;
            GUILayoutOption[] optionArray7 = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            if (GUILayout.Button(styles.sliderCycle, GUIStyle.none, optionArray7))
            {
                this.m_SliderMode = (this.m_SliderMode + 1) % ((SliderMode) 2);
                GUI.changed = true;
            }
            GUI.enabled = true;
            GUILayout.EndHorizontal();
            if (this.m_ShowSliders)
            {
                switch (this.m_SliderMode)
                {
                    case SliderMode.RGB:
                        this.RGBSliders();
                        break;

                    case SliderMode.HSV:
                        this.HSVSliders();
                        break;
                }
            }
            if (this.m_ShowAlpha)
            {
                this.m_AlphaTexture = Update1DSlider(this.m_AlphaTexture, 8, 0f, 0f, ref this.m_OldAlpha, ref this.m_OldAlpha, 3, false);
                this.m_A = this.TexturedSlider(this.m_AlphaTexture, "A", Mathf.Round(this.m_A * 255f), 0f, 255f) / 255f;
            }
            this.CalcHeight();
            if (GUI.changed)
            {
                EditorPrefs.SetInt("CPSliderShow", !this.m_ShowSliders ? 0 : 1);
                EditorPrefs.SetInt("CPSliderMode", (int) this.m_SliderMode);
                EditorPrefs.SetInt("CPColorShow", !this.m_ShowColors ? 0 : 1);
                EditorPrefs.SetInt("CPColorMode", (int) this.m_ColorBoxMode);
            }
            if (GUI.changed)
            {
                this.m_Color = new Color(this.m_R, this.m_G, this.m_B, this.m_A);
                this.SendEvent(true);
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();
            if (Event.current.type != EventType.KeyDown)
            {
                return;
            }
            KeyCode keyCode = Event.current.keyCode;
            if (keyCode != KeyCode.Return)
            {
                if (keyCode == KeyCode.Escape)
                {
                    this.m_Color = this.m_OriginalColor;
                    this.SendEvent(false);
                    base.Close();
                    GUIUtility.ExitGUI();
                }
                else if (keyCode == KeyCode.KeypadEnter)
                {
                    goto Label_0821;
                }
                return;
            }
        Label_0821:
            base.Close();
        }

        private void OnSelectionChange()
        {
            this.m_resetKeyboardControl = true;
            base.Repaint();
        }

        private void PollOSColorPicker()
        {
            if (this.m_IsOSColorPicker)
            {
                if (!OSColorPicker.visible || (Application.platform != RuntimePlatform.OSXEditor))
                {
                    UnityEngine.Object.DestroyImmediate(this);
                }
                else
                {
                    Color color = OSColorPicker.color;
                    if (this.m_Color != color)
                    {
                        this.m_Color = color;
                        this.SendEvent(true);
                    }
                }
            }
        }

        private void RGBSliders()
        {
            bool changed = GUI.changed;
            GUI.changed = false;
            this.m_RTexture = Update1DSlider(this.m_RTexture, 8, this.m_G, this.m_B, ref this.m_RTextureG, ref this.m_RTextureB, 0, false);
            this.m_GTexture = Update1DSlider(this.m_GTexture, 8, this.m_R, this.m_B, ref this.m_GTextureR, ref this.m_GTextureB, 1, false);
            this.m_BTexture = Update1DSlider(this.m_BTexture, 8, this.m_R, this.m_G, ref this.m_BTextureR, ref this.m_BTextureG, 2, false);
            float val = (int) Mathf.Round(this.m_R * 255f);
            float num2 = (int) Mathf.Round(this.m_G * 255f);
            float num3 = (int) Mathf.Round(this.m_B * 255f);
            val = this.TexturedSlider(this.m_RTexture, "R", val, 0f, 255f);
            num2 = this.TexturedSlider(this.m_GTexture, "G", num2, 0f, 255f);
            num3 = this.TexturedSlider(this.m_BTexture, "B", num3, 0f, 255f);
            if (GUI.changed)
            {
                this.m_R = val / 255f;
                this.m_G = num2 / 255f;
                this.m_B = num3 / 255f;
                this.RGBToHSV();
            }
            GUI.changed |= changed;
        }

        private void RGBToHSV()
        {
            EditorGUIUtility.RGBToHSV(new Color(this.m_R, this.m_G, this.m_B, 1f), out this.m_H, out this.m_S, out this.m_V);
        }

        private void SendEvent(bool exitGUI)
        {
            if (this.m_DelegateView != null)
            {
                Event e = EditorGUIUtility.CommandEvent("ColorPickerChanged");
                if (!this.m_IsOSColorPicker)
                {
                    base.Repaint();
                }
                this.m_DelegateView.SendEvent(e);
                if (!this.m_IsOSColorPicker && exitGUI)
                {
                    GUIUtility.ExitGUI();
                }
            }
        }

        public void SetColor(Color c)
        {
            if (this.m_IsOSColorPicker)
            {
                OSColorPicker.color = c;
            }
            else if (((this.m_Color.r != c.r) || (this.m_Color.g != c.g)) || ((this.m_Color.b != c.b) || (this.m_Color.a != c.a)))
            {
                this.m_resetKeyboardControl = true;
                this.m_Color = c;
                this.m_R = c.r;
                this.m_G = c.g;
                this.m_B = c.b;
                this.RGBToHSV();
                this.m_A = c.a;
                base.Repaint();
            }
        }

        private void SetHeight(float newHeight)
        {
            if (newHeight != base.position.height)
            {
                base.minSize = new Vector2(190f, newHeight);
                base.maxSize = new Vector2(190f, newHeight);
            }
        }

        public static void Show(GUIView viewToUpdate, Color col)
        {
            Show(viewToUpdate, col, true);
        }

        public static void Show(GUIView viewToUpdate, Color col, bool showAlpha)
        {
            ColorPicker.get.m_DelegateView = viewToUpdate;
            color = col;
            ColorPicker.get.m_OriginalColor = col;
            ColorPicker.get.m_ShowAlpha = showAlpha;
            if (ColorPicker.get.m_IsOSColorPicker)
            {
                OSColorPicker.Show(showAlpha);
            }
            else
            {
                ColorPicker get = ColorPicker.get;
                get.ShowAuxWindow();
                get.CalcHeight();
                get.title = "Color";
            }
        }

        private Vector2 Slider2D(Rect rect, Vector2 value, Vector2 maxvalue, Vector2 minvalue, GUIStyle backStyle, GUIStyle thumbStyle)
        {
            if (backStyle != null)
            {
                if (thumbStyle == null)
                {
                    return value;
                }
                int controlID = GUIUtility.GetControlID(s_Slider2Dhash, FocusType.Native);
                if (maxvalue.x < minvalue.x)
                {
                    swap(ref maxvalue.x, ref minvalue.x);
                }
                if (maxvalue.y < minvalue.y)
                {
                    swap(ref maxvalue.y, ref minvalue.y);
                }
                float height = (thumbStyle.fixedHeight != 0f) ? thumbStyle.fixedHeight : ((float) thumbStyle.padding.vertical);
                float width = (thumbStyle.fixedWidth != 0f) ? thumbStyle.fixedWidth : ((float) thumbStyle.padding.horizontal);
                Vector2 vector = new Vector2(((rect.width - (backStyle.padding.right + backStyle.padding.left)) - (width * 2f)) / (maxvalue.x - minvalue.x), ((rect.height - (backStyle.padding.top + backStyle.padding.bottom)) - (height * 2f)) / (maxvalue.y - minvalue.y));
                Rect position = new Rect((((rect.x + (value.x * vector.x)) + (width / 2f)) + backStyle.padding.left) - (minvalue.x * vector.x), (((rect.y + (value.y * vector.y)) + (height / 2f)) + backStyle.padding.top) - (minvalue.y * vector.y), width, height);
                Event current = Event.current;
                switch (current.GetTypeForControl(controlID))
                {
                    case EventType.MouseDown:
                        if (rect.Contains(current.mousePosition))
                        {
                            GUIUtility.hotControl = controlID;
                            value.x = ((((current.mousePosition.x - rect.x) - width) - backStyle.padding.left) / vector.x) + minvalue.x;
                            value.y = ((((current.mousePosition.y - rect.y) - height) - backStyle.padding.top) / vector.y) + minvalue.y;
                            GUI.changed = true;
                            Event.current.Use();
                        }
                        return value;

                    case EventType.MouseUp:
                        if (GUIUtility.hotControl == controlID)
                        {
                            GUIUtility.hotControl = 0;
                            current.Use();
                        }
                        return value;

                    case EventType.MouseMove:
                    case EventType.KeyDown:
                    case EventType.KeyUp:
                    case EventType.ScrollWheel:
                        return value;

                    case EventType.MouseDrag:
                        if (GUIUtility.hotControl == controlID)
                        {
                            value.x = ((((current.mousePosition.x - rect.x) - width) - backStyle.padding.left) / vector.x) + minvalue.x;
                            value.y = ((((current.mousePosition.y - rect.y) - height) - backStyle.padding.top) / vector.y) + minvalue.y;
                            value.x = Mathf.Clamp(value.x, minvalue.x, maxvalue.x);
                            value.y = Mathf.Clamp(value.y, minvalue.y, maxvalue.y);
                            GUI.changed = true;
                            Event.current.Use();
                            return value;
                        }
                        return value;

                    case EventType.Repaint:
                        backStyle.Draw(rect, GUIContent.none, controlID);
                        thumbStyle.Draw(position, GUIContent.none, controlID);
                        return value;
                }
            }
            return value;
        }

        private void Slider3D(Rect boxPos, Rect sliderPos, ref float x, ref float y, ref float z, GUIStyle box, GUIStyle thumb2D, GUIStyle thumbHoriz)
        {
            Rect colorBoxRect = boxPos;
            colorBoxRect.x++;
            colorBoxRect.y++;
            colorBoxRect.width -= 2f;
            colorBoxRect.height -= 2f;
            this.DrawColorSpaceBox(colorBoxRect, z);
            Vector2 vector = new Vector2(x, 1f - y);
            vector = this.Slider2D(boxPos, vector, new Vector2(0f, 0f), new Vector2(1f, 1f), box, thumb2D);
            x = vector.x;
            y = 1f - vector.y;
            Rect colorSliderRect = new Rect(sliderPos.x + 1f, sliderPos.y + 1f, sliderPos.width - 2f, sliderPos.height - 2f);
            this.DrawColorSlider(colorSliderRect, new Vector2(x, y));
            z = GUI.VerticalSlider(sliderPos, z, 1f, 0f, box, thumbHoriz);
        }

        private static void swap(ref float f1, ref float f2)
        {
            float num = f1;
            f1 = f2;
            f2 = num;
        }

        private float TexturedSlider(Texture2D background, string text, float val, float min, float max)
        {
            Rect rect = GUILayoutUtility.GetRect(16f, 16f, GUI.skin.label);
            GUI.Label(new Rect(rect.x, rect.y - 1f, 20f, 16f), text);
            rect.x += 14f;
            rect.width -= 50f;
            if (Event.current.type == EventType.Repaint)
            {
                Rect screenRect = new Rect(rect.x + 1f, rect.y + 2f, rect.width - 2f, rect.height - 4f);
                Graphics.DrawTexture(screenRect, background, new Rect(0.5f / ((float) background.width), 0.5f / ((float) background.height), 1f - (1f / ((float) background.width)), 1f - (1f / ((float) background.height))), 0, 0, 0, 0, Color.grey);
            }
            int id = EditorGUI.GetControlID(0xd42b5, EditorGUIUtility.native, base.position);
            bool changed = GUI.changed;
            GUI.changed = false;
            val = GUI.HorizontalSlider(new Rect(rect.x, rect.y + 1f, rect.width, rect.height - 2f), val, min, max, styles.pickerBox, styles.thumbHoriz);
            if (GUI.changed && EditorGUI.s_RecycledEditor.IsEditingControl(id))
            {
                EditorGUI.s_RecycledEditor.EndEditing();
            }
            Rect position = new Rect(rect.xMax + 6f, rect.y, 30f, 16f);
            val = (int) EditorGUI.DoFloatField(EditorGUI.s_RecycledEditor, position, new Rect(0f, 0f, 0f, 0f), id, val, EditorGUI.kIntFieldFormatString, EditorStyles.numberField, false);
            val = Mathf.Clamp(val, min, max);
            GUI.changed |= changed;
            return val;
        }

        private static Texture2D Update1DSlider(Texture2D tex, int xSize, float const1, float const2, ref float oldConst1, ref float oldConst2, int idx, bool hsvSpace)
        {
            if (((tex == null) || (const1 != oldConst1)) || (const2 != oldConst2))
            {
                if (tex == null)
                {
                    tex = MakeTexture(xSize, 2);
                }
                Color[] retval = new Color[xSize * 2];
                Color black = Color.black;
                Color rightGradient = Color.black;
                switch (idx)
                {
                    case 0:
                        black = new Color(0f, const1, const2, 1f);
                        rightGradient = new Color(1f, 0f, 0f, 0f);
                        break;

                    case 1:
                        black = new Color(const1, 0f, const2, 1f);
                        rightGradient = new Color(0f, 1f, 0f, 0f);
                        break;

                    case 2:
                        black = new Color(const1, const2, 0f, 1f);
                        rightGradient = new Color(0f, 0f, 1f, 0f);
                        break;

                    case 3:
                        black = new Color(0f, 0f, 0f, 1f);
                        rightGradient = new Color(1f, 1f, 1f, 0f);
                        break;
                }
                FillArea(xSize, 2, retval, black, rightGradient, new Color(0f, 0f, 0f, 0f));
                if (hsvSpace)
                {
                    HSVToRGBArray(retval);
                }
                oldConst1 = const1;
                oldConst2 = const2;
                tex.SetPixels(retval);
                tex.Apply();
            }
            return tex;
        }

        public static Color color
        {
            get
            {
                return get.m_Color;
            }
            set
            {
                get.SetColor(value);
            }
        }

        public static ColorPicker get
        {
            get
            {
                if (s_SharedColorPicker == null)
                {
                    UnityEngine.Object[] objArray = Resources.FindObjectsOfTypeAll(typeof(ColorPicker));
                    if ((objArray != null) && (objArray.Length > 0))
                    {
                        s_SharedColorPicker = (ColorPicker) objArray[0];
                    }
                    if (s_SharedColorPicker == null)
                    {
                        s_SharedColorPicker = ScriptableObject.CreateInstance<ColorPicker>();
                    }
                }
                return s_SharedColorPicker;
            }
        }

        public static bool visible
        {
            get
            {
                return (s_SharedColorPicker != null);
            }
        }

        private enum ColorBoxMode
        {
            SV_H,
            HV_S,
            HS_V,
            BG_R,
            BR_G,
            RG_B,
            EyeDropper
        }

        private enum SliderMode
        {
            RGB,
            HSV
        }

        private class Styles
        {
            public GUIStyle background = "ColorPickerBackground";
            public GUIContent colorCycle = EditorGUIUtility.IconContent("ColorPicker.CycleColor");
            public GUIStyle colorPickerBox = "ColorPickerBox";
            public GUIContent colorToggle = EditorGUIUtility.TextContent("ColorPicker.ColorFoldout");
            public GUIContent eyeDropper = EditorGUIUtility.IconContent("EyeDropper.Large");
            public GUIStyle headerLine = "IN Title";
            public GUIStyle pickerBox = "ColorPickerBox";
            public GUIContent sliderCycle = EditorGUIUtility.IconContent("ColorPicker.CycleSlider");
            public GUIContent sliderToggle = EditorGUIUtility.TextContent("ColorPicker.SliderFoldout");
            public GUIStyle thumb2D = "ColorPicker2DThumb";
            public GUIStyle thumbHoriz = "ColorPickerHorizThumb";
            public GUIStyle thumbVert = "ColorPickerVertThumb";
        }
    }
}

